/***
 * Start is the initialization program.
 * Its main (located at the bottom), will run tests if asked to do so,
 * and initialize the program by loading from the data base and storing it into
 * the ModelController.
 * 
 */

package gas.controller;

import gas.model.*;
import gas.view.*;
import java.sql.*;

class Start{
  public static ModelController modelControl = new ModelController();
  public static boolean test = false; //test boolean set when test is run as an argument.
  public static boolean wipe = false; //test boolean set when test is run as an argument.

  
  /*  
   * Appears to be Obsolete
   * 
    public static void recieveMessage(String mr){
      if ("createAccount".equals(mr)){
      }else if ("login".equals(mr)){
      }else if ("setPreferences".equals(mr)){
      }else if ("search".equals(mr)){
      }else if ("createAccount".equals(mr)){
      }else{
        //Unrecognized Signal.
      }
    }
  */ 
  /***
   * Not sure if we will use this function
   * @param user
   * @return
   */
  public static boolean registerUser(User user){
    return checkDBForUser(user.getUsername());
  }
  /***
   * Not sure if we will use this function
   * @param user
   * @return
   */
  private static boolean checkDBForUser(String un){
    try{
      Class.forName("org.sqlite.JDBC"); 
      Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
    
      Statement stat = conn.createStatement();
      
      ResultSet rs = stat.executeQuery("select * from users;");
      while (rs.next()) {
        if ( un.equals(rs.getString("name"))){
          return false;
        }
      }
      conn.close();
      return true;
    }
    catch(Exception e){
      System.err.println(e.getMessage());
      return false;
    }
  }

  
  /***
   * runTests() goes through the self Tests of all the classes
   * and informs the user when there is a failure.
   * @return
   */
  private static boolean runTests(){
      System.out.println("Running Tests");
      if ( UserBase.selfTest() ) System.out.println("UserBaseTests Passed");
      else System.err.println("Failed User Test!");
      if ( GasStationBase.selfTest() ) System.out.println("GasBaseTests Passed");
      else System.err.println("Failed Gas Test!");
      
      return true;
  }
  /**
   * initialize() initializes the GUI and the connection to the Database.
   */
  private static void initialize()throws Exception{
	    BasicGui myBasicGui = new BasicGui();
	    
	    Class.forName("org.sqlite.JDBC");
	    DatabaseController.conn = DriverManager.getConnection("jdbc:sqlite:test.db");
     
        if(wipe){
  	      DatabaseController.cleanDB();
	        DatabaseController.dummyDB(new Object());
  	    }
	    
	    DatabaseController.loadDB();
  }
  
  /**
   * Initializes the program.
   */
  public static void main(String[] args) throws Exception{
	    for (int i = 0; i < args.length; i++ ){
	      if ( "test".equals(args[i]) ){
	        test = true;
	    	runTests();       
	      }else if ( "wipe".equals(args[i]) ){
	          wipe = true;
	      }
	    }
	    initialize();
  }
  
}
